import { resolve } from 'path';

import FX from 'fs-extra';

import { dirData } from '../../../lib/global.js';

import { pad0, wadHash } from './lib/Tool.js';


const genPathSoundBank = function(usage, lang, champion, index, type, format, version = '2016') {
	return `assets/sounds/wwise${version}/${usage}/${usage == 'vo' ? `${lang}/` : ''}characters/${champion}/skins/${index}/${champion}_${index}_${usage}_${type}.${format}`;
};

const genArrPathSoundBank = function(usage, lang, champion, index, version = '2016') {
	return [
		[genPathSoundBank(usage, lang, champion, index, 'audio', 'wpk', version), `${usage}_${index}_audio.wpk`],
		[genPathSoundBank(usage, lang, champion, index, 'audio', 'bnk', version), `${usage}_${index}_audio.bnk`],
		[genPathSoundBank(usage, lang, champion, index, 'events', 'bnk', version), `${usage}_${index}_event.bnk`],
	];
};


const base = FX.readJSONSync(resolve(dirData, 'zh_cn.json'));


export default function makeFilesHash(slot, lang) {
	const CP = Object.values(base).find(champion => champion.slot.toLowerCase() == slot.toLowerCase());

	if(!CP) { return []; }

	const filesHash = {};

	const infoFiles = [];

	const idMax = Math.max(...Object.values(CP.skins).filter(skin => typeof skin == 'object').map(skin => skin.id)) + 30;
	for(let id = 0; id <= idMax; id++) {
		infoFiles.push(...genArrPathSoundBank('vo', lang, CP.slot, `skin${pad0(id, 2)}`));
	}

	infoFiles.push(...genArrPathSoundBank('vo', lang, CP.slot, 'base'));


	for(const [pathSoundBank, nameFile] of infoFiles) {
		filesHash[wadHash(pathSoundBank)] = nameFile;
	}


	return filesHash;
}